package com.xbongbong.saas.enums.business;

import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.ScreenTypeEnum;
import com.xbongbong.saas.enums.SetTypeEnum;
import com.xbongbong.saas.enums.ShowTypeEnum;
import lombok.Getter;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 报价单枚举
 * @author long.rao
 * @version v1.0
 * @date 2019/7/24 14:09
 * @since v1.0
 */
@Getter
public enum QuotationEnum {
    // 报价单模板设置字段
    // 基本信息
    BASE_INFO("other_1", "enum.quotation.attrName.base_info", "baseInfo", FieldTypeEnum.SEPARATOR.getType(), SetTypeEnum.DISABLE_REPEAT.getCode(), 1, 0, ShowTypeEnum.EDIT_DETAIL.getCode(), 10000, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    QUOTATION_NO("serialNo", "enum.quotation.attrName.quotation_no", "quotationNo", FieldTypeEnum.SERIALNO.getType(), 1, 1, 0, ShowTypeEnum.ALL.getCode(), 9990, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    NAME("text_1", "enum.quotation.attrName.name", "name", FieldTypeEnum.TEXT.getType(), 4, 0, 0, ShowTypeEnum.ALL.getCode(), 9980, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    CUSTOMER_ID("text_2", "enum.quotation.attrName.customer_id", "customerId", FieldTypeEnum.LINK_BUSINESS_SINGLE.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9970, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    CONTACT_ID("text_3", "enum.quotation.attrName.contact_id", "contactId", FieldTypeEnum.LINK_BUSINESS_SINGLE.getType(), 3, 0, 0, ShowTypeEnum.ALL.getCode(), 9960, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    OPPORTUNITY_ID("text_4", "enum.quotation.attrName.opportunity_id", "opportunityId", FieldTypeEnum.LINK_BUSINESS_SINGLE.getType(), 3, 0, 0, ShowTypeEnum.ALL.getCode(), 9950, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    QUOTATION_PERSON("text_5", "enum.quotation.attrName.quotation_person", "quotationPerson", FieldTypeEnum.USER.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9940, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    QUOTATION_TIME("date_1", "enum.quotation.attrName.quotation_time", "quotationTime", FieldTypeEnum.DATETIME.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9930, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    EFFECTIVE_TIME("date_2", "enum.quotation.attrName.effective_time", "effectiveTime", FieldTypeEnum.DATETIME.getType(), 3, 0, 0, ShowTypeEnum.ALL.getCode(), 9920, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    STATUS("text_6", "enum.quotation.attrName.status", "status", FieldTypeEnum.COMBO.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9910, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    OWNER_ID("ownerId", "enum.quotation.attrName.owner_id", "ownerId", FieldTypeEnum.OWNERID.getType(), 1, 1, 0, ShowTypeEnum.ALL.getCode(), 9900, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    MINOR_USER("coUserId", "enum.quotation.attrName.minor_user", "coUserId", FieldTypeEnum.COUSERID.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9890, 1, 0, ScreenTypeEnum.SCREEN_SEARCH.getType()),
    // 销售信息
    SALE_INFO("other_2", "enum.quotation.attrName.sale_info", "saleInfo", FieldTypeEnum.SEPARATOR.getType(), SetTypeEnum.DISABLE_REPEAT.getCode(), 1, 0, ShowTypeEnum.EDIT_DETAIL.getCode(), 9880, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    PRODUCT("array_1", "enum.quotation.attrName.product", "products", FieldTypeEnum.SELECT_PRODUCT.getType(), 3, 0, 0, ShowTypeEnum.ALL.getCode(), 9870, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    PRODUCT_TOTAL("num_1", "enum.quotation.attrName.product_total", "productTotal", FieldTypeEnum.NUM.getType(), 3, 1, 0, ShowTypeEnum.ALL.getCode(), 9860, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    DISCOUNT("num_2", "enum.quotation.attrName.discount", "discount", FieldTypeEnum.NUM.getType(), 3, 1, 0, ShowTypeEnum.ALL.getCode(), 9850, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    OTHER_EXPENSE("num_3", "enum.quotation.attrName.other_expense", "otherExpense", FieldTypeEnum.NUM.getType(), 3, 1, 0, ShowTypeEnum.ALL.getCode(), 9840, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    DISCOUNT_AMOUNT("num_4", "enum.quotation.attrName.discount_amount", "discountAmount", FieldTypeEnum.NUM.getType(), 3, 1, 0, ShowTypeEnum.ALL.getCode(), 9830, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    AMOUNT("num_5", "enum.quotation.attrName.amount", "amount", FieldTypeEnum.NUM.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9820, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    ESTIMATE_COST("num_6", "enum.quotation.attrName.estimate_cost", "estimateCost", FieldTypeEnum.NUM.getType(),SetTypeEnum.DISABLE_OPEN.getCode(), 0, 0, ShowTypeEnum.ALL.getCode(), 9810, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    GROSS_PROFIT("num_7", "enum.quotation.attrName.gross_profit", "grossProfit", FieldTypeEnum.NUM.getType(), SetTypeEnum.DISABLE_OPEN.getCode(), 0, 0, ShowTypeEnum.ALL.getCode(), 9800, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    GROSS_PROFIT_RATE("num_8", "enum.quotation.attrName.gross_profit_rate", "grossProfitRate", FieldTypeEnum.NUM.getType(), SetTypeEnum.DISABLE_OPEN.getCode(), 0, 0, ShowTypeEnum.ALL.getCode(), 9790, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    // 扩展信息
    EXTEND_INFO("other_3", "enum.quotation.attrName.extend_info", "extendInfo", FieldTypeEnum.SEPARATOR.getType(), SetTypeEnum.DISABLE_REPEAT.getCode(), 1, 0, ShowTypeEnum.EDIT_DETAIL.getCode(), 9780, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    // 系统信息
    SYSTEM_INFO("other_4", "enum.quotation.attrName.system_info", "systemInfo", FieldTypeEnum.SEPARATOR.getType(), SetTypeEnum.DISABLE_REPEAT.getCode(), 1, 0, ShowTypeEnum.EDIT_DETAIL.getCode(), 9760, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    CREATOR_ID("creatorId", "enum.quotation.attrName.creator_id", "creatorId", FieldTypeEnum.CREATORID.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9750, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    ADD_TIME("addTime", "enum.quotation.attrName.add_time", "addTime", FieldTypeEnum.ADDTIME.getType(), 1, 1, 0, ShowTypeEnum.ALL.getCode(), 9740, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    UPDATE_TIME("updateTime", "enum.quotation.attrName.update_time", "updateTime", FieldTypeEnum.UPDATETIME.getType(), 1, 1, 0, ShowTypeEnum.ALL.getCode(), 9730, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    // 逻辑字段
    VERSION("text_8", "enum.quotation.attrName.version", "version", FieldTypeEnum.TEXT.getType(), SetTypeEnum.DISABLE_ALL.getCode(), 0, 1, ShowTypeEnum.NONE.getCode(), 9720, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    ARCHIVED("num_10", "enum.quotation.attrName.archived", "archived", FieldTypeEnum.COMBO_NUM.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9710, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    CUSTOMER_NAME("text_9", "enum.quotation.attrName.customer_name", "customerName", FieldTypeEnum.TEXT.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9700, 1, 1, ScreenTypeEnum.SCREEN_OFF.getType()),
    CONTACT_NAME("text_10", "enum.quotation.attrName.contact_name", "contactName", FieldTypeEnum.TEXT.getType(), 3, 0, 1, ShowTypeEnum.NONE.getCode(), 9690, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    OPPORTUNITY_NAME("text_11", "enum.quotation.attrName.opportunity_name", "opportunityName", FieldTypeEnum.TEXT.getType(), 3, 0, 1, ShowTypeEnum.NONE.getCode(), 9680, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    QUOTATION_NAME("text_13", "enum.quotation.attrName.quotation_name", "quotationName", FieldTypeEnum.TEXT.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9660, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    GROUP_NUMBER("text_14", "enum.quotation.attrName.group_number", "groupNumber", FieldTypeEnum.TEXT.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9650, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    IS_NEW("num_9", "enum.quotation.attrName.is_new", "isNew", FieldTypeEnum.COMBO_NUM.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9640, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    ;

    /**
     * 英文名称
     */
    private String attr;
    /**
     * 中文名称
     */
    private String attrName;
    /**
     * 对应saas中的attr
     */
    private String saasAttr;
    /**
     * 字段类型，详见
     *
     * @see com.xbongbong.paas.enums.FieldTypeEnum
     */
    private Integer fieldType;
    /**
     * 重命名、判重、启用必填等权限
     * 1.重命名+字段权限
     * 2.重命名+字段权限+查重
     * 3.重命名+字段权限+启用、必填
     * 4.重命名+字段权限+查重+启用、必填
     * 注：pro中启用和必填是同一个权限，同时启用或同时关闭
     */
    private Integer setType;
    /**
     * 模板设计阶段字段是否允许编辑
     * 1：允许编辑
     * 0：不允许编辑
     */
    private Integer designEditable;
    /**
     * 字段是否显示在模板设计界面
     * 1：不显示
     * 0：显示
     */
    private Integer editHide;
    /**
     * 字段在各个界面（新建、编辑、列表、详情）的展现形式
     * 详见
     *
     * @see com.xbongbong.saas.enums.ShowTypeEnum
     */
    private Integer showType;
    /**
     * 字段在模板设计界面的排序
     * 建议不要用连续的数字，可以用等差数列，以防以后在两个字段之间加入新字段
     */
    private Integer sort;
    /**
     * 字段在各个界面是否可见(启用)
     * 1：可见
     * 0：不可见
     * 注：字段在具体页面的可见性需要用属性和showType联合判断，在visible=1的情况下判断showType
     */
    private Integer visible;
    /**
     * 字段是否必填
     * 1：必填
     * 0：不必填
     */
    private Integer required;
    /**
     * 筛选类型
     * 1：可筛选
     * 2：不可筛选
     */
    private Integer screenType;

    QuotationEnum(String attr, String attrName, String saasAttr, Integer fieldType, Integer setType, Integer designEditable,
                 Integer editHide, Integer showType, Integer sort, Integer visible, Integer required, Integer screenType) {
        this.attr = attr;
        this.attrName = attrName;
        this.saasAttr = saasAttr;
        this.fieldType = fieldType;
        this.setType = setType;
        this.designEditable = designEditable;
        this.editHide = editHide;
        this.showType = showType;
        this.sort = sort;
        this.visible = visible;
        this.required = required;
        this.screenType = screenType;
    }


    /**
     * app端详情默认标题显示字段
     *
     * @return List
     * @author long.rao
     * @date 2019-08-12 23:27
     */
    public static List<Object> getTitleAttrList() {
        return Collections.singletonList(QUOTATION_NO);
    }

    /**
     * 默认标签
     *
     * @author long.rao
     * @date 2019-06-27 15:36
     */
    public static List<Object> getLabelAttrList() {
        return Collections.singletonList(STATUS);
    }

    /**
     * 详情默认摘要
     *
     * @author long.rao
     * @date 2019-06-27 15:47
     */
    public static List<Object> getSummaryAttrList() {
        return Arrays.asList(CUSTOMER_ID,NAME,QUOTATION_PERSON,QUOTATION_TIME);
    }

    /**
     * 对data内的字段拼上“data.”
     *
     * @param cache
     * @return java.lang.String
     * @author long.rao
     * @date 2019-08-23 13:50
     */
    public static String getAttrConnectData(QuotationEnum cache) {
        return "data." + cache.getAttr();
    }

    /**
     * 获取es查询、聚合字段
     * data在库内是json对象，对data字段内某个key做es【精确查询】时，需要在其前拼上“data.”，且【text等类型的】要在后面拼“.keyword”
     *
     * @param cache 当前枚举对象
     * @return java.lang.String 处理后的聚合字段
     * @author long.rao
     * @date 2019-08-23 13:50
     */
    public static String getEsAttr4Keyword(QuotationEnum cache) {
        FieldTypeEnum fieldTypeEnum = FieldTypeEnum.getFieldTypeEnum(cache.fieldType);
        //字段类型为text，或者字段在es内的类型时text，则需要在后面拼上“.keyword”
        boolean flag = fieldTypeEnum == FieldTypeEnum.TEXT || "text".equals(fieldTypeEnum.getEsalias());
        if (flag) {
            return "data." + cache.getAttr() + ".keyword";
        }
        return "data." + cache.getAttr();
    }

    /**
     * 把传入的字段（类型为LINK_BUSINESS_SINGLE）的转为其隐藏字段
     * @param attr 字段名
     * @return java.lang.String
     * @author long.rao
     * @date 2019-10-23 21:40
     */
    public static String changeToHide4Link(String attr) {
        if (Objects.equals(attr, CUSTOMER_ID.attr)) {
            return CUSTOMER_NAME.attr;
        } else if (Objects.equals(attr, CONTACT_ID.attr)) {
            return CONTACT_NAME.attr;
        } else if (Objects.equals(attr, OPPORTUNITY_ID.attr)) {
            return OPPORTUNITY_NAME.attr;
        }
        return attr;
    }

    public String getAttrName() {
        return I18nMessageUtil.getMessage(attrName);
    }

    public static String getI18nValue(String value) {
        return I18nMessageUtil.getMessage(value);
    }
}
